<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 自定义组件，标题 -->
      <PageTitle :show-before="true">
        <template v-slot:before>
          <h2>举报信息</h2>
        </template>
      </PageTitle>
      <el-row :gutter="10" type="flex" style="margin-top: 20px;">
        <!-- <el-col :span="6"></el-col> -->
        <el-col :span="4">
          <el-select
            placeholder="举报类型：职位"
            @change="handleReportTypeChange"
            v-model="reportQuery.type"
          >
            <el-option
              v-for="item in reportType"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="4">
          <el-select
            placeholder="不限举报原因"
            clearable
            @change="handleReportReasonChange"
            v-model="reportQuery.reason"
          >
            <el-option
              v-for="item in reportReason"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="4">
          <el-select
            placeholder="不限核实情况"
            clearable
            @change="handleVerifySituationChange"
            v-model="reportQuery.status"
          >
            <el-option
              v-for="item in verifySituation"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-col>
      </el-row>
      <!-- 数据表 -->
      <el-table
        v-loading="downloadLoading"
        ooltip-effect="dark"
        :data="reportList"
        max-height="500px"
        style="margin-top: 20px"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" fixed />
        <el-table-column
          label="举报对象"
          align="center"
          width="width"
          prop="reportedName"
          fixed
        />
        <el-table-column
          label="举报原因"
          align="center"
          prop="content"
          width="width"
          fixed
        >
        </el-table-column>
        <el-table-column align="center" prop="status" label="处理情况">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status ? 'success' : 'danger'">{{
              scope.row.status ? "已处理" : "未处理"
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="举报者手机号"
          align="center"
          prop="mobile"
          width="width"
        />
        <el-table-column label="举报时间" align="center" prop="addtime">
          <template slot-scope="{ row }">
            {{ row.addtime | dateFormat }}
          </template>
        </el-table-column>

        <el-table-column
          :label="$t('table.actions')"
          width="width"
          align="center"
          fixed="right"
        >
          <template slot-scope="{ row }">
            <el-button type="danger" size="mini" @click="handleDelete(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-row :gutter="10" style="margin-top: 20px">
        <el-col :span="8">
          <el-button type="primary" size="small" @click="handleClick"
            >处理</el-button
          >
          <el-button type="danger" size="small" @click="handleBatchDelete"
            >删除所选</el-button
          >
        </el-col>
        <el-col :span="16">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="reportQuery.page"
            :page-sizes="[10, 20, 30, 50]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            style="text-align: right"
          >
          </el-pagination>
        </el-col>
      </el-row>
    </div>

    <!-- 处理弹层 -->
    <el-dialog
      title="将所选信息设置为"
      :visible.sync="handleDialog"
      width="25%"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="处理情况">
          <el-radio v-model="radio" label="0">未处理</el-radio>
          <el-radio v-model="radio" label="1">已处理</el-radio>
        </el-form-item>
        <el-row :gutter="10" type="flex" justify="end">
          <el-button @click="handleDialog = false">取消</el-button>
          <el-button type="primary" @click="btnOk">确定</el-button>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  selReportListApi,
  delReportByIdsApi,
  updateReportStatusByIdsApi,
} from "@/api/manageUser";
export default {
  data() {
    return {
      reportQuery: {
        page: 1,
        pageSize: 10,
        type: 1,
        reason: null,
        status: null,
      },
      reportType: [
        { id: 1, name: "举报类型：职位" },
        { id: 2, name: "举报类型：简历" },
      ],
      reportReason: [
        { id: "", name: "不限举报原因" },
        { id: 1, name: "电话虚假(空号、无人接听)" },
        { id: 2, name: "职介收费" },
        { id: 3, name: "职介冒充" },
        { id: 4, name: "虚假(职位、待遇等)" },
        { id: 5, name: "网赚虚假(刷钻、刷单)" },
        { id: 6, name: "其他" },
      ],
      verifySituation: [
        { id: "", name: "不限核实情况" },
        { id: 1, name: "属实" },
        { id: 2, name: "不属实" },
        { id: 3, name: "未核实" },
      ],
      reportList: [],
      selectedStatuses: [],
      ids: [],
      handleDialog: false,
      radio: "0",
    };
  },
  created() {
    this.selReportList();
  },
  methods: {
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.selectedStatuses = selection.map((item) => item.status);
      console.log('选中的ids:', this.ids);
      console.log(this.selectedStatuses);
    },
    // 列表渲染
    async selReportList() {
      const res = await selReportListApi(this.reportQuery);
      console.log(res);
      this.reportList = res.records;
      this.total = res.total;
    },
    // 审核
    handleClick() {
      if (this.ids.length === 0) {
        this.$message.warning("请选择要处理的数据");
        return;
      }
      this.handleDialog = true;
    },
    // 举报删除
    handleDelete(id) {
      this.ids.push(id);
      this.$confirm("此操作将永久删除该举报信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await delReportByIdsApi(this.ids);
          console.log(res);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          if (res.code === 1) {
            this.$message.success("删除成功!");
            this.selReportList();
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch(() => {});
    },
    // 点击确定
    async btnOk() {
      try {
        console.log(this.ids);
        const res = await updateReportStatusByIdsApi(this.ids, this.radio);
        console.log(res);
        if (res.code === 1) {
          this.$message.success("处理成功!");
          this.ids = [];
          this.handleDialog = false;
          this.selReportList();
        } else {
          this.$message.error("处理失败");
        }
      } catch {
        this.$message.error("处理失败");
      }
    },
    // 举报类型
    handleReportTypeChange(val) {
      console.log(val);
      this.reportQuery.page = 1;
      this.selReportList();
    },
    // 举报原因
    handleReportReasonChange(val) {
      console.log(val);
      this.reportQuery.page = 1;
      this.selReportList();
    },
    // 核实情况
    handleVerifySituationChange(val) {
      console.log(val);
      this.reportQuery.page = 1;
      this.selReportList();
    },
    // 每页条数
    handleSizeChange(val) {
      this.reportQuery.pageSize = val;
      this.selReportList();
    },
    // 当前页
    handleCurrentChange(val) {
      this.reportQuery.page = val;
      this.selReportList();
    },
    // 新增：批量删除方法
    handleBatchDelete() {
      if (this.ids.length === 0) {
        this.$message.warning("请选择要删除的数据");
        return;
      }

      this.$confirm('此操作将永久删除选中的举报信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          const res = await delReportByIdsApi(this.ids);
          if (res.code === 1) {
            this.$message.success('删除成功!');
            this.selReportList();
            this.ids = [];
          } else {
            this.$message.error(res.msg || '删除失败');
          }
        } catch (error) {
          console.error('删除失败:', error);
          this.$message.error('删除失败');
        }
      }).catch(() => {
        // 取消删除操作
      });
    },
  },
};
</script>

<style scoped></style>
